package com.library.controller;

import com.library.service.BookService;
import com.library.service.BorrowRecordService;
import com.library.service.UserService;
import com.library.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/statistics")
@Slf4j
public class StatisticsController {

    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    @Autowired
    private BorrowRecordService borrowRecordService;

    @GetMapping("/dashboard")
    @PreAuthorize("hasRole('ADMIN')")
    public ResultVO<Map<String, Object>> getDashboardData() {
        try {
            Map<String, Object> data = new HashMap<>();

            long bookCount = bookService.count();
            data.put("bookCount", bookCount);

            long userCount = userService.count();
            data.put("userCount", userCount);

            long borrowedCount = 0;
            data.put("borrowedCount", borrowedCount);

            long availableCount = 0;
            data.put("availableCount", availableCount);

            Map<String, Long> categoryStats = new HashMap<>();
            categoryStats.put("LITERATURE", 0L);
            categoryStats.put("TECHNOLOGY", 0L);
            categoryStats.put("HISTORY", 0L);
            categoryStats.put("ART", 0L);
            categoryStats.put("OTHER", 0L);
            data.put("categoryStats", categoryStats);

            List<Map<String, Object>> borrowTrend = new ArrayList<>();
            borrowTrend.add(createMap("date", "2024-01-01", "count", 5));
            borrowTrend.add(createMap("date", "2024-01-02", "count", 8));
            borrowTrend.add(createMap("date", "2024-01-03", "count", 12));
            borrowTrend.add(createMap("date", "2024-01-04", "count", 6));
            borrowTrend.add(createMap("date", "2024-01-05", "count", 9));
            borrowTrend.add(createMap("date", "2024-01-06", "count", 15));
            borrowTrend.add(createMap("date", "2024-01-07", "count", 10));
            data.put("borrowTrend", borrowTrend);

            List<Map<String, Object>> popularBooks = new ArrayList<>();
            popularBooks.add(createMap("title", "Java核心技术", "borrowCount", 45));
            popularBooks.add(createMap("title", "Spring Boot实战", "borrowCount", 38));
            popularBooks.add(createMap("title", "JavaScript高级程序设计", "borrowCount", 32));
            popularBooks.add(createMap("title", "算法导论", "borrowCount", 28));
            popularBooks.add(createMap("title", "设计模式", "borrowCount", 25));
            data.put("popularBooks", popularBooks);

            return ResultVO.success(data);
        } catch (Exception e) {
            log.error("获取统计数据失败: {}", e.getMessage());
            return ResultVO.error("获取统计数据失败: " + e.getMessage());
        }
    }

    @GetMapping("/categories")
    public ResultVO<Map<String, Long>> getCategoryStatistics() {
        try {
            Map<String, Long> categoryStats = new HashMap<>();
            categoryStats.put("LITERATURE", 0L);
            categoryStats.put("TECHNOLOGY", 0L);
            categoryStats.put("HISTORY", 0L);
            categoryStats.put("ART", 0L);
            categoryStats.put("OTHER", 0L);

            return ResultVO.success(categoryStats);
        } catch (Exception e) {
            log.error("获取分类统计失败: {}", e.getMessage());
            return ResultVO.error("获取分类统计失败: " + e.getMessage());
        }
    }

    private Map<String, Object> createMap(Object... keyValues) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < keyValues.length; i += 2) {
            map.put(String.valueOf(keyValues[i]), keyValues[i + 1]);
        }
        return map;
    }
}
